package cn.kgc.mcartoon.service.impl;

import cn.kgc.mcartoon.dto.ResultDto;
import cn.kgc.mcartoon.service.FileUpLoadService;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.domain.ThumbImageConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class FileUpLoadServiceImpl implements FileUpLoadService {

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private ThumbImageConfig thumbImageConfig;

    private Logger log = LoggerFactory.getLogger(this.getClass());


    @Override
    public ResultDto upload(MultipartFile file) throws IOException {

        long fileSize = file.getSize();
        if(fileSize >(1024 * 1024 * 10)){
            return ResultDto.error("图片尺寸太大");
        }

        //获取文件名
        String originalFilename = file.getOriginalFilename();

        log.info("file:{}",file.getOriginalFilename());

        //截取文件的后缀
        String ext = StringUtils.substringAfterLast(originalFilename, ".");

        storageClient.uploadFile(file.getInputStream(),file.getSize(),ext,null);
//
//
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), ext, null);
//
        Map<String,String> map = new HashMap<>();
        map.put("url",storePath.getFullPath());
        map.put("slt_url",thumbImageConfig.getThumbImagePath(storePath.getPath()));
        return ResultDto.ok(map);
    }
}
